<style>
  .header-font {
    font-size: 15px;
    font-weight: bold;
  }
  .line {
    margin: 10px 0;
    width: 100%;
    border: 0.5px solid rgb(218, 218, 218);
  }
  .table {
    margin-top: 10px;
  }
  .paging {
    float:right;
    margin-top:10px;
  }
</style>
<template>
  <div>
    <div class='header-font'>
      <Icon type="md-apps" />
      <span>商品列表</span>
    </div>
    <div class="line"></div>
    <product-list-form
      class="product-analysis-form"
      @on-success-valid="handleSubmit">
    </product-list-form>
    <Button
      icon="md-add"
      type="primary"
      @click="handleClick">
      添加商品
    </Button>
    <product-list-modal
      @product-list-valid="handleEdit">
    </product-list-modal>
    <delete-modal
      @delete-valid="handleConfirmDelete">
    </delete-modal>
    <Table
      class="table"
      :columns="columns"
      :data="historyData"
      :loading="loading"
      border>
    </Table>
    <table-page
      class="paging"
      :dataCount="dataCount"
      @change-page="changePage"
      @change-page-size="changePageSize"
      :pageSize="pageSize">
    </table-page>
  </div>
</template>
<script>
import {
  mapActions,
  mapState
} from 'vuex'
import ProductListModal from '_c/product-list-modal'
import ProductListForm from '_c/product-list-form'
import DeleteModal from '_c/delete-modal'
import TablePage from '_c/table-page'
export default {
  components: {
    ProductListForm,
    ProductListModal,
    DeleteModal,
    TablePage
  },
  data () {
    return {
      page: 1,
      title: '爆品营销商品列表',
      id: null,
      rows: 20,
      loading: true,
      confirmData: {},
      columns: [
        {
          title: '序号',
          type: 'index2',
          width: 80,
          align: 'center',
          render: (h, params) => {
            return h('span', params.index + (this.page - 1) * this.pageSize + 1)
          }
        },
        {
          title: '商品名称',
          key: 'name',
          minWidth: 120,
          align: 'center'
        },
        {
          title: '参考价格',
          key: 'price',
          minWidth: 120,
          align: 'center'
        },
        {
          title: '创建日期',
          key: 'ctime',
          minWidth: 150,
          align: 'center'
        },
        {
          title: '状态',
          key: 'state',
          align: 'center',
          minWidth: 120,
          render: (h, params) => {
            return h(
              'span',
              {},
              params.row.state === 0 ? '正常' : params.row.state === 1 ? '不可用' : '-'
            )
          }
        },
        {
          title: '备注',
          key: 'des',
          minWidth: 150,
          align: 'center',
          render: (h, params) => {
            return h('span', params.row.des ? params.row.des : '-')
          }
        },
        {
          title: '操作',
          key: 'cz',
          minWidth: 150,
          align: 'center',
          render: (h, params) => {
            if (params.row.state === 0) {
              return h('div', [
                // h('Button', {
                //   props: {
                //     type: 'primary',
                //     size: 'small',
                //     ghost: 'ghost'
                //   },
                //   style: {
                //     marginRight: '5px'
                //   },
                //   on: {
                //     click: () => {
                //       this.handleClick(params.row.id, params.row)
                //     }
                //   }
                // }, '编辑'),
                h('Button', {
                  props: {
                    type: 'error',
                    size: 'small',
                    ghost: 'ghost'
                  },
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: () => {
                      this.handleDelete(params.row.id)
                    }
                  }
                }, '弃用')
              ])
            } else if (params.row.state === 1) {
              return h('div', [
                // h('Button', {
                //   props: {
                //     type: 'primary',
                //     size: 'small',
                //     ghost: 'ghost'
                //   },
                //   style: {
                //     marginRight: '5px'
                //   },
                //   on: {
                //     click: () => {
                //       this.handleClick(params.row.id, params.row)
                //     }
                //   }
                // }, '编辑'),
                h('Button', {
                  props: {
                    type: 'primary',
                    size: 'small',
                    ghost: 'ghost'
                  },
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: () => {
                      this.handleOpen(params.row.id)
                    }
                  }
                }, '开启')
              ])
            }
          }
        }
      ]
    }
  },
  computed: {
    ...mapState({
      goodServiceList: state => state.goodService.goodServiceList,
      dataCount: state => state.goodService.goodServiceDataCount,
      historyData: state => state.goodService.goodServiceHistoryData,
      pageSize: state => state.goodService.goodServicePageSize,
      row: state => state.goodService.row
    })
  },
  methods: {
    ...mapActions([
      'getGoodServiceList',
      'getGoodServiceCurrentRow',
      'editGoodServiceData',
      'deleteGoodServiceData',
      'updateModalState',
      'updateDeleteModalState',
      'changeGoodServicePage',
      'changeGoodServicePageSize'
    ]),
    handleSubmit (playload) {
      this.loading = true
      this.playload = playload
      this.getGoodServiceList({
        ...playload,
        rows: this.rows
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    },
    handleClick (id, row) {
      let newRow = {}
      // this.id = id
      if (row) {
        newRow = Object.assign({}, row)
      }
      this.getGoodServiceCurrentRow(newRow)
      // console.log(this.row)
      this.updateModalState(true)
    },
    async handleDelete (id) {
      // console.log(id)
      this.id = id
      this.loading = true
      const resp = await this.editGoodServiceData({
        id: id,
        state: 1
      })
      if (resp.data.state === '000') {
        this.$Message.success('商品已弃用')
        this.getGoodServiceList({
          page: this.page,
          rows: this.rows
        }).then((res) => {
          if (res.data.state === '000') {
            this.loading = false
          }
        })
      } else {
        this.$Message.error(resp.data.msg)
      }
      // this.updateDeleteModalState(true)
    },
    async handleOpen (id) {
      this.id = id
      this.loading = true
      const resp = await this.editGoodServiceData({
        id: id,
        state: 0
      })
      if (resp.data.state === '000') {
        this.$Message.success('商品已开启')
        this.getGoodServiceList({
          page: this.page,
          rows: this.rows
        }).then((res) => {
          if (res.data.state === '000') {
            this.loading = false
          }
        })
      } else {
        this.$Message.error(resp.data.msg)
      }
    },
    handleConfirmDelete () {
      // console.log(this.id)
      // this.lodaing = true
      // this.editGoodServiceData({ state: 1 }).then(
      //   this.getGoodServiceList({
      //     page: this.page,
      //     rows: this.rows
      //   }).then((res) => {
      //     if (res.data.state === '000') {
      //       this.loading = false
      //     }
      //   })
      // )
      this.updateDeleteModalState(false)
      this.changePage(this.page)
    },
    async handleEdit (playload) {
      // console.log(playload)
      this.loading = true
      const resp = await this.editGoodServiceData(playload)
      if (resp.data.state === '000') {
        this.$Message.success('添加商品成功')
        this.getGoodServiceList({
          page: this.page,
          rows: this.rows
        }).then((res) => {
          if (res.data.state === '000') {
            this.loading = false
          }
        })
      } else {
        this.$Message.error(resp.data.msg)
      }
      this.updateModalState(false)
      // this.updateConfirmModalState(true)
    },
    changePage (index) {
      this.lodaing = true
      this.page = index
      this.changeGoodServicePage(index)
      this.getGoodServiceList({
        page: index,
        rows: this.rows
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    },
    changePageSize (value) {
      this.loading = true
      this.rows = value
      this.changeGoodServicePageSize(value)
      this.changPage(this.page)
      this.getGoodServiceList({
        rows: value
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    }
  },
  created () {
    this.getGoodServiceList({}).then((res) => {
      if (res.data.state === '000') {
        this.loading = false
      }
    })
  }
}
</script>
